<?php
/*
*	2012年6月5日21:44:21
*	用户操作模型
*	袁吉
*/
class MemberModel extends Model{
	var $usecookie = true; // 是否使用cookies(自动登录)
	var $err_username	= 2; // 用户名不存在
	var $err_password	= 0; // 密码错误
	var $login_success	= 1;//登录成功
	var $u_code = 'U_CODE';//cookies标示
	#用户登录
	#返回 0用户名或者密码错误，1 成功，2不存在用户名
	public function login($name,$password,$usecookie=true){
		$member = $this->where(array('domain'=>$name))->field('id,domain,uname,email,password')->find();
		$this->usecookie = $usecookie;
		if(empty($member))return $this->err_username;
		if($this->password === md5(md5($password).'比尔白吃')){
			session('u_name',$this->uname);
			session('u_id',$this->id);
			session('u_domain',$this->domain);
			session('u_email',$this->email);
			$this->updateCookies();
			return $this->login_success;
		}else{
			return $this->err_password;
		}
	}
	#检查用户登录状态,已登录返回true，未登录或者登录超时返回false
	public function isLoggedin(){//如果有session，返回true，没有再判断是否需要自动登录！
		$u_id = session('u_id');
		if(!empty($u_id)){
			return true;
		}
		$u_code = cookie($this->u_code);
		if(!empty($u_code)){
			$data = json_decode($this->_decode($u_code),true);
			if($this->login($data['domain'],$data['password'],false) == $this->login_success){
				return true;
			}
		}
		return false;
	}
	#更新cookie
	public function updateCookies(){
		$u_code=$this->_encode(json_encode($this->data));
		if($this->usecookie){//记住密码
			cookie($this->u_code,$u_code);
		}else{
			cookie($this->u_code,$u_code,0);//退出浏览器者失效
		}
	}
	#退出登录
	public function logout(){
		session(NULL);
		cookie($this->u_code,NULL);
	}
	
	#解密用户cookie
	private function _decode($str){		
		return $str = mcrypt_decrypt( MCRYPT_DES, '袁吉的密码key',$str, MCRYPT_MODE_ECB );
	}
	#加密用户cookie
	private function _encode($str){
		return $str = mcrypt_encrypt ( MCRYPT_DES, '袁吉的密码key',$str, MCRYPT_MODE_ECB );
	}
	#更具二级域名获取会员id
	#以后要改成使用缓存
	public function getMemberIdByDomain($domain){
		$id = $this->where(array('domain'=>$domain))->getField('id');
		return $id;
	}
	#更具域名获取主页
	public function getHomePageByDomain($domain){
		$member_id = $this->getMemberIdByDomain($domain);
		if(empty($member_id)){
			return false;
		}
		$HomePage = D('HomePage');
		$home_page_list = $HomePage->where(array('member_id'=>$member_id,'status'=>1))->order('sort_order')->select();
		$format_home_page_list=array();
		foreach($home_page_list as $home_page){
			$home_page['icon_url'] = formatIconUrl($home_page['url']);//设置图标
			$format_home_page_list[$home_page['id']] = $home_page;
		}
		return $format_home_page_list;
	}
	#更具域名获取首页导航数据列表
	public function getSiteNavByDomain($domain){
		$member_id = $this->getMemberIdByDomain($domain);
		if(empty($member_id)){
			return false;
		}
		$SiteNav = D('SiteNav');
		$site_nav_list = $SiteNav->where(array('member_id'=>$member_id,'status'=>1))->order('sort_order')->select();
		$format_site_nav_list=array();
		foreach($site_nav_list as $site_nav){
			$site_nav['icon_url'] = formatIconUrl($site_nav['url']);//设置图标
			$format_site_nav_list[$site_nav['id']] = $site_nav;
		}
		return $format_site_nav_list;
	}
	#更具域名获取用户设置的搜索引擎
	public function getSearchEngine($domain){
		$search_engine_config = C('search_engine_config');//获取默认配置
		$SearchEngine_list = S(__CLASS__.__FUNCTION__.$domain);
		if(is_array($SearchEngine_list)){
			return $SearchEngine_list;
		}
		//$list = $this->where(array('status'=>1))->order('id DESC')->group('type')->select();
		$search_engine = $this->where(array("domain"=>$domain))->getField('search_engine');
		if(!empty($search_engine)){//如果用户有对搜索引擎的设置
			$search_engine = json_decode($search_engine,true);
			foreach($search_engine as $key=>$vo){
				$search_engine_config[$key]=$vo;
			}
		}
		//print_r($search_engine_config);
		$search_engine_config = implode(',',$search_engine_config);
		$list = D('SearchEngine')->where(array('id'=>array('in',$search_engine_config)))->select();
		//print_r($list);
		$SearchEngine_list = array();
		foreach($list as $SearchEngine){
			$SearchEngine_list[$SearchEngine['type']]=$SearchEngine;
		}
		S(__CLASS__.__FUNCTION__.$domain,$SearchEngine_list ,C('SearchEngine_list_stime'));
		//print_r($SearchEngine_list );
		return $SearchEngine_list;
	}
}